package com.sl.hx.shop.utils;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.Toast;

/**
 * Toast工具类
 *
 * author hwg
 * date 2017/10/23
 */

public class ToastUtil {

    //默认显示
    private static boolean isShow = true ;

    //全局唯一的Toast
    private static Toast mToast = null ;

    /**
     * 私有化构造方法
     */
    private ToastUtil(){
        throw new UnsupportedOperationException("不能被实例化") ;
    }

    /**
     * 控制是否全局显示
     *
     * @param isShowToast
     */
    public static void controlShow(boolean isShowToast){
        isShow = isShowToast ;
    }

    /**
     * 取消Toast显示
     */
    public void cancelToast(){
        if (mToast != null && isShow){
            mToast.cancel();
        }
    }

    /**
     * 短时间提示Toast
     *
     * @param context
     * @param message
     */
    public static void showShort(Context context , CharSequence message){
        if (isShow){
            if (mToast == null){
                mToast = Toast.makeText(context.getApplicationContext(), message , Toast.LENGTH_SHORT) ;
            }else{
                mToast.setText(message);
            }
            mToast.show();
        }
    }

    /**
     * 长时间提示Toast
     *
     * @param context
     * @param message
     */
    public static void showLong(Context context , CharSequence message){
        if (isShow){
            if (mToast == null){
                mToast = Toast.makeText(context.getApplicationContext() , message , Toast.LENGTH_LONG) ;
            }else{
                mToast.setText(message);
            }
            mToast.show();
        }
    }

    /**
     * 自定义提示时间的Toast
     *
     * @param context
     * @param message
     * @param duration
     */
    public static void showCustomTime(Context context , CharSequence message , int duration){
        if (isShow){
            if (mToast == null){
                mToast = Toast.makeText(context.getApplicationContext() , message , duration) ;
            }else{
                mToast.setText(message);
            }
            mToast.show();
        }
    }

    /**
     * 自定义Toast布局
     *
     * @param context
     * @param message
     * @param view
     */
    public static void showCustomView(Context context , CharSequence message , View view){
        if (isShow){
            if (mToast == null){
                mToast = Toast.makeText(context.getApplicationContext() , message , Toast.LENGTH_SHORT) ;
            }else{
                mToast.setText(message);
            }

            if (view != null){
                mToast.setView(view);
            }

            mToast.setGravity(Gravity.CENTER, 0, 0);

            mToast.show();

        }
    }

    /**
     * 自定义Toast布局，并居中显示
     *
     * @param context
     * @param message
     * @param view
     */
    public static void showCustomViewCenter(Context context, CharSequence message, View view){
        if (isShow){
            if (mToast == null){
                mToast = Toast.makeText(context.getApplicationContext() , message , Toast.LENGTH_SHORT) ;
            }else{
                mToast.setText(message);
            }

            if (view != null){
                mToast.setView(view);
            }

            mToast.setGravity(Gravity.CENTER, 0,0);

            mToast.show();
        }
    }

}

